﻿#include "slider.h"
#include <QPainter>
#include <QMouseEvent>
#include <QFontMetrics>
#include <QLinearGradient>

Slider::Slider(QWidget *parent)
    : QWidget(parent), m_foregroundColor(QColor(0, 120, 215)), m_backgroundColor(QColor(240, 240, 240)),
      m_handleColor(QColor(0, 120, 215)), m_textColor(Qt::white), m_borderRadius(5), m_textFontSize(10),
      m_minValue(0), m_maxValue(100), m_value(0), m_tickMarksVisible(true) {
    setMinimumSize(200, 50); // 设置最小尺寸
}

void Slider::setForegroundColor(const QColor &color) {
    m_foregroundColor = color;
    update();
}

void Slider::setBackgroundColor(const QColor &color) {
    m_backgroundColor = color;
    update();
}

void Slider::setHandleColor(const QColor &color) {
    m_handleColor = color;
    update();
}

void Slider::setBorderRadius(int radius) {
    m_borderRadius = radius;
    update();
}

void Slider::setTextColor(const QColor &color) {
    m_textColor = color;
    update();
}

void Slider::setTextFontSize(int size) {
    m_textFontSize = size;
    update();
}

void Slider::setTickMarksVisible(bool visible) {
    m_tickMarksVisible = visible;
    update();
}

void Slider::setRange(int min, int max) {
    if (min >= max) return; // 确保最小值小于最大值
    m_minValue = min;
    m_maxValue = max;
    setValue(m_value); // 更新当前值以确保在范围内
}

void Slider::setValue(int value) {
    value = qBound(m_minValue, value, m_maxValue); // 确保值在范围内
    if (m_value != value) {
        m_value = value;
        update();
        emit valueChanged(m_value); // 发出值改变信号
    }
}

int Slider::value() const {
    return m_value;
}

void Slider::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    painter.setPen(Qt::NoPen);
    painter.setBrush(m_backgroundColor);
    painter.drawRoundedRect(rect(), m_borderRadius, m_borderRadius);

    // 绘制前景（进度条）
    int progressWidth = valueToPosition(m_value);
    QRect progressRect(0, 0, progressWidth, height());
    painter.setBrush(m_foregroundColor);
    painter.drawRoundedRect(progressRect, m_borderRadius, m_borderRadius);

    // 绘制刻度
    if (m_tickMarksVisible) {
        painter.setPen(QPen(m_textColor, 1));
        for (int i = m_minValue; i <= m_maxValue; i += (m_maxValue - m_minValue) / 10) {
            int x = valueToPosition(i);
            painter.drawLine(x, height() - 10, x, height());
        }
    }

    // 绘制滑块（正圆形）
    int handleSize = height(); // 滑块直径与高度相同
    int handleX = progressWidth - handleSize / 2; // 滑块中心位置
    handleX = qBound(0, handleX, width() - handleSize); // 确保滑块在范围内
    QRect handleRect(handleX, 0, handleSize, handleSize);
    painter.setBrush(m_handleColor);
    painter.setPen(Qt::NoPen);
    painter.drawEllipse(handleRect);

    // 绘制进度文本（居中显示）
    QString progressText = QString("%1").arg(m_value);
    QFont font = painter.font();
    font.setPointSize(m_textFontSize); // 设置字体大小
    painter.setFont(font);
    painter.setPen(m_textColor);

    // 计算文本位置（居中）
    QFontMetrics metrics(font);
    int textWidth = metrics.horizontalAdvance(progressText);
    int textHeight = metrics.height();
    QRect textRect(handleRect.center().x() - textWidth / 2, handleRect.center().y() - textHeight / 2, textWidth, textHeight);
    painter.drawText(textRect, Qt::AlignCenter, progressText);
}

void Slider::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
        updateValueFromMouse(event->pos());
    }
}

void Slider::mouseMoveEvent(QMouseEvent *event) {
    if (event->buttons() & Qt::LeftButton) {
        updateValueFromMouse(event->pos());
    }
}

void Slider::resizeEvent(QResizeEvent *event) {
    Q_UNUSED(event);
    update();
}

void Slider::updateValueFromMouse(const QPoint &pos) {
    int newValue = positionToValue(pos.x());
    setValue(newValue);
}

int Slider::valueToPosition(int value) const {
    return ((value - m_minValue) * width()) / (m_maxValue - m_minValue);
}

int Slider::positionToValue(int position) const {
    return m_minValue + ((position * (m_maxValue - m_minValue)) / width());
}
